package za.org.jcicada.datastore.impl;

import java.net.URI;
import java.util.Date;

import za.org.jcicada.datastore.IDataStoreClient;
import za.org.jcicada.datastore.IDataStoreServer;
import za.org.jcicada.genss.messages.Result;
import za.org.jcicada.genss.messages.SuccessCode;
import za.org.jcicada.genss.sensor.ISensorValue;
import za.org.jcicada.genss.sensor.SensorURN;
import za.org.jcicada.genss.sensor.impl.SensorStatusValue;

public abstract class DataStoreServerAbstract implements IDataStoreServer {
	
	public void initialize(URI connectionURI) throws Exception {
		// do nothing
	}

	public abstract void storeSensor(SensorURN sensorURN, ISensorValue value);
	
	public abstract void storeSensorStatus(SensorURN sensorURN, SensorStatusValue statusValue);
	
	public final void requestSensorHistory(IDataStoreClient client, SensorURN sensorURN, Date from, Date to) {
		if (from == null || to == null) {
			client.replySensorHistory(new Result(SuccessCode.COMPLETE_FAILURE, "From or to dates can't be null"), sensorURN, from, to, null);
			return;
		}
		if (from.after(to)) {
			client.replySensorHistory(new Result(SuccessCode.COMPLETE_FAILURE, "From and to dates are reversed"), sensorURN, from, to, null);
			return;
		}
		retrieveSensorHistory(client, sensorURN, from, to);
	}
	
	public final void requestStatusHistory(IDataStoreClient client, SensorURN sensorURN, Date from, Date to) {
		if (from == null || to == null) {
			client.replyStatusHistory(new Result(SuccessCode.COMPLETE_FAILURE, "From or to dates can't be null"), sensorURN, from, to, null);
			return;
		}
		if (from.after(to)) {
			client.replyStatusHistory(new Result(SuccessCode.COMPLETE_FAILURE, "From and to dates are reversed"), sensorURN, from, to, null);
			return;
		}
		retrieveStatusHistory(client, sensorURN, from, to);
	}
	
	public abstract void retrieveSensorHistory(IDataStoreClient client, SensorURN sensorURN, Date from, Date to);
	
	public abstract void retrieveStatusHistory(IDataStoreClient client, SensorURN sensorURN, Date from, Date to);

	public void informDisconnect(IDataStoreClient client) {
		// do nothing
	}

}
